package com.linln.common.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

//import org.springframework.web.context.request.RequestContextHolder;

public class SessionUtil {
    /**
     * SpringMvc下获取request
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        /*HttpServletRequestWrapper wrapper=new HttpServletRequestWrapper();
        RequestContextUtils.getRequest()
        RequestContextHolder
        HttpServletRequestWrapper wrapper=new HttpServletRequestWrapper(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
        */
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;

    }

    public static HttpSession getSession() {
        HttpServletRequest request = getRequest();
        HttpSession session = request.getSession();
        return session;

    }

    public static String getToken() {
//        return (String) getRequest().getAttribute("token");
        return (String) getRequest().getHeader("token");
    }
    public static Long getUserId() {
        return Long.valueOf(JwtUtil.getUserId(SessionUtil.getToken()));
    }
}
